package com.google.coffeeshop.gui.desktop.orderpayment.seatdetail;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import com.google.coffeeshop.business.OrderBusiness;
import com.google.coffeeshop.entity.ProductDTO;
import com.google.coffeeshop.entity.ProductGroupDTO;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.gui.desktop.AbstractBasePanel;
import com.google.coffeeshop.gui.desktop.orderpayment.ItemButton;

public class MenuProductPanel extends AbstractBasePanel{
	static public String PRODUCT_IMAGE_PACKAGE = "/com/google/coffeeshop/gui/desktop/images/products/";
	private SeatDTO seat;
	private long employeeId;
	private SeatDetailFrame parent;
	
	public MenuProductPanel(SeatDetailFrame parent, SeatDTO seat, Long employeeId) {
		this.parent = parent;
		this.seat = seat;
		this.employeeId = employeeId;
		setLayout(new BorderLayout(0, 0));
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		add(tabbedPane);
		
		addAllProductsOnGUI(tabbedPane);
	}
	
	private void addAllProductsOnGUI(JTabbedPane tabbedPane) {
		List<ProductGroupDTO> productGroups = OrderBusiness.findAllProductGroups();
		if (productGroups != null && !productGroups.isEmpty()) {
			for (ProductGroupDTO productGroup : productGroups) {
				try {
					JPanel pnlProductGroup = createProductGroupPanel();
					addProductGroupOnGUI(productGroup, pnlProductGroup);
					tabbedPane.addTab(productGroup.getName(), null, pnlProductGroup, null);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	private JPanel createProductGroupPanel() {
		JPanel panel = new JPanel();
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0};
		gbl_panel.rowHeights = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		return panel;
	}

	private void addProductGroupOnGUI(ProductGroupDTO productGroup, JPanel pnlProductGroup) throws IOException {
		List<ProductDTO> products = OrderBusiness.findAllProductsByGroupId(productGroup.getId());
		if (products != null && !products.isEmpty()) {
			for (int number = 0; number < products.size(); number++) {
				GridBagConstraints gbc = createGridBagConstraints(number);
				JButton itemButton = createProductPanel(products.get(number));
				if (itemButton != null) {
					pnlProductGroup.add(itemButton, gbc);
				}
			}
		}
	}
	
	private GridBagConstraints createGridBagConstraints(int number) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = 0;
		gbc.gridy = number;
		return gbc;
	}
	
	private JButton createProductPanel(ProductDTO product) throws IOException {
		URL url = MenuProductPanel.class.getResource(PRODUCT_IMAGE_PACKAGE + product.getImageFilename());
		ItemButton itemButton = null;
		if (url != null) {
			itemButton = new ItemButton(product, url);
			addClickEventForItemButton(itemButton);
		}
		return itemButton;
	}
	
	private void addClickEventForItemButton(JButton itemButton) {
		itemButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ItemButton button = (ItemButton)e.getSource();
				OrderBusiness.orderStuff(seat.getId(), employeeId, button.getProduct().getId(), 1);
				parent.refreshParentGUI();
			}
		});
	}
	
	private static final long serialVersionUID = 1L;
}
